package com.example.psysystemhomework;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.example.psysystemhomework.bean.BackBean;
import com.example.psysystemhomework.bean.UserBean;
import com.example.psysystemhomework.util.AppManager;
import com.example.psysystemhomework.util.NetUtil;
import com.example.psysystemhomework.util.ShareUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Random;

import butterknife.ButterKnife;

public class LoginActivity extends AppCompatActivity {


    private TextView mTextview;
    private EditText mEdittextnumber2;
    private Button mButton;
    private String allBack = "{}";
    private LinearLayout lineBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        setOnclick();


    }


    private void setOnclick() {
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendLogin(mEdittextnumber2.getText().toString());
            }

        });
    }

    private void sendLogin(String phone) {
        UserBean userBean = new UserBean();
        userBean.setPhone(phone);
        Thread thread = new Thread() {
            @Override
            public void run() {
                NetUtil netUtil = new NetUtil(getApplicationContext());
                Gson gson = new Gson();
                String data = gson.toJson(userBean);
                String s = netUtil.loginUser(data);
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    if (jsonObject.getBoolean("flag")) {
                        Log.i("登录请求用户查询", "run: " + s);
                        EventBus.getDefault().post(s);
                    } else {
                        EventBus.getDefault().post("fail");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


//                super.run();
            }
        };
        thread.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onBackPressed() {
        AppManager appManager = AppManager.getAppManager();
        appManager.finishActivity();
        super.onBackPressed();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String s) throws JSONException {
        if (s.equals("getBack")) {
            Log.i("背景图片加载", "onEvent: " + s);
            JSONObject jsonObject = new JSONObject(allBack);
            String data = jsonObject.get("data").toString();
            Gson gson = new Gson();
            List<BackBean> o = gson.fromJson(data, new TypeToken<List<BackBean>>() {
            }.getType());
            Random random = new Random();
            Glide.with(this)
                    .load(o.get(random.nextInt(o.size())).getImageUrl())
                    .asBitmap()
                    .into(new SimpleTarget<Bitmap>(180, 180) {//设置宽高
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                            Drawable drawable = new BitmapDrawable(resource);
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                lineBack.setBackground(drawable);//设置背景
                            }
                        }
                    });


        } else if (s.equals("fail")) {
            Toast.makeText(this, "登录请求失败", Toast.LENGTH_SHORT).show();

        } else if(s.length()>30) {
            if (s.isEmpty()) {
                Toast.makeText(this, "请求失败", Toast.LENGTH_SHORT).show();
            } else {

                Gson gson = new Gson();
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    UserBean userBean = gson.fromJson(jsonObject.get("data").toString(), UserBean.class);

                    if (userBean.getStatus() == 0) {
                        Toast.makeText(this, "等待管理员审核", Toast.LENGTH_SHORT).show();
                    } else {
                        ShareUtil shareUtil = new ShareUtil(this);
                        shareUtil.saveUser(s);
                        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                        onBackPressed();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }



            }
        }


    }

    private void initViews() {
        mTextview = findViewById(R.id.textView);
        mEdittextnumber2 = findViewById(R.id.editTextNumber2);
        mButton = findViewById(R.id.button);
        lineBack = findViewById(R.id.lineBack);


        getBack();
    }

    private void getBack() {
        NetUtil netUtil = new NetUtil(getApplicationContext());
        new Thread() {
            @Override
            public void run() {
                allBack = netUtil.getAllBack().toString();
                EventBus.getDefault().post("getBack");
                super.run();
            }
        }.start();
    }
}